package com.wf.course.web.controller.course;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wf.course.model.course.CourseShare;
import com.wf.course.web.Constants;
import com.wf.course.web.OSSUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.utils.page.PageBean;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseComment;
import com.wf.course.model.member.Member;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseCommentService;
import com.wf.course.service.IMemberService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 课程评论列表Controller
 */
@RequestMapping("courseComment")
@Controller
public class CourseCommentController {

	@Autowired
	private ICourseCommentService courseCommentService;
	@Autowired
	private ICourseChapterService courseChapterService;
	@Autowired
	private IMemberService memberService;

	/**
	 * 课程评论列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param chapterName
	 * @param userName
	 * @param model
	 * @return
	 */
	@LogInfo("查询课程评论信息列表")
	@RequestMapping("courseCommentList")
	public String chapterList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
 			@RequestParam(value = "chapterName", required = false) String chapterName, 
			@RequestParam(value = "userName", required = false) String userName, 
			@RequestParam(value = "commentType", required = false) String commentType, 
			HttpSession session,
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("chapterName", chapterName);
		cons.put("userName", userName);
		cons.put("commentType", commentType);
 		
		PageBean<CourseComment> pageBean = new PageBean<CourseComment>(pageNo, pageSize, null, cons);
		pageBean.setUrl("courseComment/courseCommentList");
		this.courseCommentService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		session.setAttribute("page", pageBean);
		return "course/courseCommentList";
	}

	/**
	 * 评论修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("添加/修改课程评论，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			CourseComment courseComment = courseCommentService.queryCourseCommentById(id);
			model.addAttribute("courseComment", courseComment);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		//章节内容-
		List<CourseChapter> chapters = courseChapterService.queryCourseChapters();
		 model.addAttribute("chapters",chapters);
		 
		 List<Member> members = memberService.queryMembers();
		 model.addAttribute("members",members);
		
		return "course/courseCommentEdit";
	}
	
	/**
	 * 加载回复页面
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("加载回复页面")
	@RequestMapping("DownLoadReply/{id}")
	public String DownLoadReply(@PathVariable("id") Integer id,Model model){
		CourseComment courseComment = courseCommentService.queryAllCourseCommentById(id);
		model.addAttribute("courseComment",courseComment);
		
		//章节内容-
		/*List<CourseChapter> chapters = courseChapterService.queryCourseChapters();
		model.addAttribute("chapters",chapters);
		 
		List<Member> members = memberService.queryMembers();
		model.addAttribute("members",members);*/
		return "course/courseCommentReply";
	}

	/**
	 * 评论新增和修改
	 * 
	 * @param courseComment
	 * @param request
	 * @return
	 */
	@LogInfo("添加/修改课程评论")
	@RequestMapping(value = "courseCommentEdit" , produces = "text/html;charset=UTF-8")
	public String courseCommentEdit(@ModelAttribute CourseComment courseComment, HttpServletRequest request,HttpSession httpSession,Model model) {
		if (courseComment.getReplyFile() != null && courseComment.getReplyFile().getSize() != 0) {
			// 重新上传
			String imgPath = "";
			try {
				imgPath = OSSUtil.addFile(courseComment.getReplyFile(), "course_comment", Constants.IGM_BUCKET);
			} catch (IOException e) {
				e.printStackTrace();
			}
			courseComment.setReplyImage(imgPath);
		}
		courseCommentService.saveOrUpdateCourseComment(courseComment);
		//保存后，跳转到之前的列表页
		@SuppressWarnings("unchecked")
		PageBean<T> page =  (PageBean<T>) httpSession.getAttribute("page");
		
		PageBean<CourseComment> pageBean = new PageBean<CourseComment>(page.getActulPageNo(), page.getPageSize(), null, page.getConMap());
		pageBean.setUrl("courseComment/courseCommentList");
		this.courseCommentService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "course/courseCommentList";
	}

	/**
	 * 课程删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除课程评论")
	@RequestMapping(value = "remove/{id}")
	public String removeCourseComment(@PathVariable("id") Integer id) {
		
		courseCommentService.deleteById(id);
		
		return "redirect:/courseComment/courseCommentList";
	}
	
	
	/**
	 * 删除课程评论
	 * @param ids
	 * @return
	 */
	@LogInfo("批量删除课程评论")
	@ResponseBody
	@RequestMapping(value = { "batchdeleteComment" }, method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public String deleteByParamIds(@RequestParam(value = "ids[]", required = false) String[] ids) {
		if(ids!=null && ids.length>0){
				try {
					if(!courseCommentService.batchdeleteComment(ids))
						throw new Exception();
				} catch (Exception e) {
					e.printStackTrace();
					return "0";
				}
			}
		
		return "1";
		
	}
	
	@ResponseBody
	@LogInfo("设置评论为置顶")
	@RequestMapping(value="setTop",method=RequestMethod.GET,produces="text/html;chartset=UTF-8")
	public String setTop(@RequestParam("id") Integer id){
		boolean b =  courseCommentService.setTop(id);
		return b?"1":"0";
	}

}
